<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 在搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryinfo.query"
            class="input-with-select"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="serach"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsdata" border style="width: 100%">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="580">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column label="创建时间">
          <!-- 利用作用域插槽修饰时间 -->
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsserach: '',
      goodsdata: [],
      // 商品总条数
      total: 0,
      // 存储时间的
      // 获取商品列表请求参数
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 20
      }
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryinfo
      })
      //   console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      //   res.data.goods.forEach(item => {
      //       item.upd_time
      //   })
      this.goodsdata = res.data.goods
      this.total = res.data.total
    },
    // 查询商品 原理是用input的监听输入的内容到请求参数里面。同时发送请求
    serach() {
      this.getlist()
    },
    // 添加商品
    addGoods() {
      this.$router.push('goods/add')
    },
    // 实现分页的功能
    // 当页的大小改变时触发 当前页展示多少条数据
    handleSizeChange(newSize) {
      //   console.log(newPage)
      this.queryinfo.pagesize = newSize
      this.getlist()
    },
    // 当页变动时候触发的事件。 第几页
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryinfo.pagenum = newPage
      this.getlist()
    },
    // 商品的删除
    async deleteGoods(row) {
      console.log(row)
      // 询问用户是否必须删除
      const Result = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (Result !== 'confirm') {
        return this.$message.info('已经取消了删除')
      }
      // 进行删除操作
      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除信息失败')
      }
      this.getlist()
      this.$message.success('删除成功')
    }
  }
}
</script>

<style lang="less" scoped>
</style>